
using System.Linq;
using Admin3000.Backend.Api.Entity;
using Admin3000.Backend.Api.Params;
using Admin3000.Backend.Api.Repository;
using Admin3000.Backend.Api.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Admin3000.Backend.Api.Controllers
{
    // [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class ArticleCommentsController : ControllerBase
    {
        private IConfiguration _configuration;

        private IRepository<ArticleComments> _articlecommentsRepository;
        private IRepository<UserComment> _usercommentRepository;

        private IRepository<Articles> _articlesRepository;

        private IRepository<Comments> _commentsRepository;

        private IRepository<Users> _usersRepository;

        private TokenParameter _tokenParameter;

        public ArticleCommentsController(
            IConfiguration configuration,
            IRepository<ArticleComments> articlecommentsRepository,
            IRepository<Comments> commentsRepository,
            IRepository<UserComment> usercommentRepository,
            IRepository<Users> usersRepository
        )
        {
            _configuration = configuration;
            _articlecommentsRepository = articlecommentsRepository;
            _commentsRepository = commentsRepository;
            _usercommentRepository = usercommentRepository;
            _usersRepository = usersRepository;
            _tokenParameter =
                _configuration
                    .GetSection("TokenParameter")
                    .Get<TokenParameter>();
        }


        // //通过文章id获取评论id
        // [HttpPost,Route("acticleid")]
        // public dynamic POST(CreateArticleComments createarticlecomments)
        // {
        //     var acticleid=createarticlecomments.ArticleId;
        //     var articlecomment=_articlecommentsRepository.Table.Where(x=>x.ArticleId==acticleid).ToList();

        // return new{
        //     Code=1000,
        //     Data=articlecomment,
        //     Msg="查询指定用户Id成功"
        // };
        // }

        //通过文章id获取评论id
        // [HttpGet("articleid/{articleid}"),Route("articleid")]
        // public dynamic Get(int articleid)
        // {
        //     var acticlecomment=_articlecommentsRepository.Table.Where(x=>x.ArticleId==articleid).ToList();

        //     return new{
        //         Code=1000,
        //         Data=acticlecomment,
        //         Msg="查询指定用户Id成功"
        //     };

        // }




        //列出文章评论列表
        [HttpGet]
        public dynamic Get()
        {
            var articlecomment = _articlecommentsRepository.Table.ToList();

            {

            }
            // var kescc = query.

            var articlecomments = new
            {
                Code = 1000,
                Data = new { Data = articlecomment },
                Msg = "列出文章评论列表成功"
            };

            return articlecomments;
        }

        //通过文章id查询文章评论表
        [HttpGet("articleid/{articleid}"), Route("articleid")]
        public dynamic Get(int articleid)
        {
            var acticlecomment = _articlecommentsRepository.Table.Where(x => x.ArticleId == articleid).ToList();
            var comments = _commentsRepository.Table.ToList();
            var usercomment = _usercommentRepository.Table.ToList();
            var user = _usersRepository.Table.ToList();
            var query = from a in acticlecomment
                        join b in comments on a.CommentId equals b.Id
                        join c in usercomment on b.Id equals c.CommentId
                        join d in user on c.UserId equals d.Id
                        select new
                        {
                            ArticleId = a.ArticleId,
                            CommentId = a.CommentId,
                            CommentText = b.CommentText,
                            UserId = c.UserId,
                            Username = d.Username
                        };
            return new
            {
                Code = 1000,
                Data = new { Data = query },
                Msg = "查询指定用户Id成功"
            };

        }




        //增加文章评论 文章Id 评论Id
        [HttpPost]
        [Route("articlecommentsID")]
        public dynamic Post(CreateArticleComments newArticleComments)
        {
            var articlecomments = new ArticleComments
            {
                ArticleId = newArticleComments.ArticleId,
                CommentId = newArticleComments.CommentId
            };
            _articlecommentsRepository.Insert(articlecomments);
            return new
            {
                Code = 1000,
                Data = articlecomments,
                Msg = "增加文章评论成功"
            };
        }


        //删除对应Id文章评论
        [HttpDelete("{id}")]
        public dynamic Delete(int id)
        {
            _articlecommentsRepository.Delete(id);

            return new { Code = 1000, Data = "", Msg = "删除文章评论成功" };

        }




    }
}
